//? 1函数声明
// 要在函数输入输出 都要进行类型定义
// (1)函数声明
function sum(x: number, y: number): number {
  return x + y;
}
// (2)函数表达式
let mySum = function (x: number, y: number): number {
  return x + y;
};
// 这时候,相当于给了匿名函数进行类型定义 mySum进行赋值操作推断而来
let mySum2: (x: number, y: number) => number = function (
  x: number,
  y: number
): number {
  return x + y;
};

// ? 用接口来定义函数
interface SearchFunc {
  (source: string, subString): boolean;
}

let mySearch: SearchFunc;

mySearch = function (source: string, subString: string) {
  return source.search(subString) !== -1;
};

// ?可选参数
// 可选参数后面不能加必选参数
// 报错
// function buildName(firstname?:string,lastname:string):string {
//   if (firstname) {
//     return firstname + ' '+lastname;
//   }
// }
// ?参数默认值
// 此时不受[可选参数必选接在必需参数后面]的限制
function buildName(firstName?: string, lastName: string = "Cat") {
  return firstName + " " + lastName;
}
let tomcat = buildName("Tom", "Cat");
let tom = buildName("Tom");


// ?剩余参数
function push(array: any[], ...items: any[]) {
  items.forEach(function(item) {
      array.push(item);
  });
}

let a = [];
push(a, 1, 2, 3);